﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace WINADM.Server.WMI
{
    public class Win32Service
    {
        Boolean? _AcceptPause;

        public Boolean? AcceptPause
        {
            get { return _AcceptPause; }
            set { _AcceptPause = value; }
        }
        Boolean? _AcceptStop;

        public Boolean? AcceptStop
        {
            get { return _AcceptStop; }
            set { _AcceptStop = value; }
        }
        string _Caption;

        public string Caption
        {
            get { return _Caption; }
            set { _Caption = value; }
        }
        UInt32? _CheckPoint;

        public UInt32? CheckPoint
        {
            get { return _CheckPoint; }
            set { _CheckPoint = value; }
        }
        string _CreationClassName;

        public string CreationClassName
        {
            get { return _CreationClassName; }
            set { _CreationClassName = value; }
        }
        string _Description;

        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
        Boolean? _DesktopInteract;

        public Boolean? DesktopInteract
        {
            get { return _DesktopInteract; }
            set { _DesktopInteract = value; }
        }
        string _DisplayName;

        public string DisplayName
        {
            get { return _DisplayName; }
            set { _DisplayName = value; }
        }
        string _ErrorControl;

        public string ErrorControl
        {
            get { return _ErrorControl; }
            set { _ErrorControl = value; }
        }
        UInt32? _ExitCode;

        public UInt32? ExitCode
        {
            get { return _ExitCode; }
            set { _ExitCode = value; }
        }
        DateTime? _InstallDate;

        public DateTime? InstallDate
        {
            get { return _InstallDate; }
            set { _InstallDate = value; }
        }
        string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        string _PathName;

        public string PathName
        {
            get { return _PathName; }
            set { _PathName = value; }
        }
        UInt32? _ProcessId;

        public UInt32? ProcessId
        {
            get { return _ProcessId; }
            set { _ProcessId = value; }
        }
        UInt32? _ServiceSpecificExitCode;

        public UInt32? ServiceSpecificExitCode
        {
            get { return _ServiceSpecificExitCode; }
            set { _ServiceSpecificExitCode = value; }
        }
        ushort _ServiceType;

        public ushort ServiceType
        {
            get { return _ServiceType; }
            set { _ServiceType = value; }
        }
        Boolean? _Started;

        public Boolean? Started
        {
            get { return _Started; }
            set { _Started = value; }
        }
        ushort _StartMode;

        public ushort StartMode
        {
            get { return _StartMode; }
            set { _StartMode = value; }
        }
        string _StartName;

        public string StartName
        {
            get { return _StartName; }
            set { _StartName = value; }
        }
        ushort _State;

        public ushort State
        {
            get { return _State; }
            set { _State = value; }
        }
        string _Status;

        public string Status
        {
            get { return _Status; }
            set { _Status = value; }
        }
        string _SystemCreationClassName;

        public string SystemCreationClassName
        {
            get { return _SystemCreationClassName; }
            set { _SystemCreationClassName = value; }
        }
        string _SystemName;

        public string SystemName
        {
            get { return _SystemName; }
            set { _SystemName = value; }
        }
        UInt32? _TagId;

        public UInt32? TagId
        {
            get { return _TagId; }
            set { _TagId = value; }
        }
        UInt32? _WaitHint;

        public UInt32? WaitHint
        {
            get { return _WaitHint; }
            set { _WaitHint = value; }
        }

        public Win32Service()
        {

        }
        public Win32Service(ManagementObject wmiWin32ServiceObject)
        {
            this._AcceptPause = (bool)wmiWin32ServiceObject["AcceptPause"];
            this._AcceptStop = (bool)wmiWin32ServiceObject["AcceptStop"];
            this._Caption = (string)wmiWin32ServiceObject["Caption"];
            this._CheckPoint =(UInt32?) wmiWin32ServiceObject["CheckPoint"];
            this._CreationClassName = (string)wmiWin32ServiceObject["CreationClassName"];
            this._Description = (string)wmiWin32ServiceObject["Description"];
            this._DesktopInteract = (bool)wmiWin32ServiceObject["DesktopInteract"];
            this._DisplayName = (string)wmiWin32ServiceObject["DisplayName"];
            this._ErrorControl = (string)wmiWin32ServiceObject["ErrorControl"];
            this._InstallDate = WMIUtility.ConvertDmtfToDateTime(wmiWin32ServiceObject["InstallDate"]);
            this._Name = (string)wmiWin32ServiceObject["Name"];
            this._PathName = (string)wmiWin32ServiceObject["PathName"];
            this._ProcessId = (UInt32?)wmiWin32ServiceObject["ProcessId"];
            this._ServiceSpecificExitCode = (UInt32?)wmiWin32ServiceObject["ServiceSpecificExitCode"];
            this._ServiceType = GetServiceTypeId((string)wmiWin32ServiceObject["ServiceType"]);
            this._Started = (bool)wmiWin32ServiceObject["Started"];
            this._StartMode = GetServiceStartModeId((string)wmiWin32ServiceObject["StartMode"]);
            this._StartName = (string)wmiWin32ServiceObject["StartName"];
            this._State = GetServiceStatusId((string)wmiWin32ServiceObject["State"]);
            this._Status = (string)wmiWin32ServiceObject["Status"];
            this._SystemCreationClassName = (string)wmiWin32ServiceObject["SystemCreationClassName"];
            this._SystemName = (string)wmiWin32ServiceObject["SystemName"];
            this.TagId = (UInt32?)wmiWin32ServiceObject["TagId"];
            this.WaitHint = (UInt32?)wmiWin32ServiceObject["WaitHint"];
        }

        public ushort GetServiceStartModeId(string value)
        {
            if (value != null)
            {
                switch (value.Trim())
                {
                    case "Boot":
                        {
                            return 1;
                        }
                    case "System":
                        {
                            return 2;
                        }
                    case "Auto":
                        {
                            return 3;
                        }
                    case "Manual":
                        {
                            return 4;
                        }
                    case "Disabled":
                        {
                            return 5;
                        }
                }
            }
            return 6;
        }

        public ushort GetServiceStatusId(string value)
        {
            if (value != null)
            {
                switch (value.Trim())
                {
                    case "Stopped":
                        {
                            return 1;
                        }
                    case "Start Pending":
                        {
                            return 2;
                        }
                    case "Stop Pending":
                        {
                            return 3;
                        }
                    case "Running":
                        {
                            return 4;
                        }
                    case "Continue Pending":
                        {
                            return 5;
                        }
                    case "Pause Pending":
                        {
                            return 6;
                        }
                    case "Paused":
                        {
                            return 7;
                        }
                    case "Unknown":
                        {
                            return 8;
                        }
                }
            }
            return 8;
        }
        public ushort GetServiceTypeId(string value)
        {

            if (value != null)
            {
                switch (value.Trim())
                {
                    case "Kernel Driver":
                        {
                            return 1;
                        }
                    case "File System Driver":
                        {
                            return 2;
                        }
                    case "Adapter":
                        {
                            return 3;
                        }
                    case "Recognizer Driver":
                        {
                            return 4;
                        }
                    case "Own Process":
                        {
                            return 5;
                        }
                    case "Share Process":
                        {
                            return 6;
                        }
                    case "Interactive Process":
                        {
                            return 7;
                        }
                }
            }
            return 8;
        }
    }
}
